<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Creates a prefix/ns context for the next XPath query</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="simplexmlelement.getnamespaces.html">« SimpleXMLElement::getNamespaces</a></li>
      <li style="float: right;"><a href="simplexmlelement.savexml.html">SimpleXMLElement::saveXML »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.simplexmlelement.html">SimpleXMLElement</a></li>
    <li>Creates a prefix/ns context for the next XPath query</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="simplexmlelement.registerxpathnamespace" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SimpleXMLElement::registerXPathNamespace</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">SimpleXMLElement::registerXPathNamespace</span> &mdash; <span class="dc-title">
   Creates a prefix/ns context for the next XPath query
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-simplexmlelement.registerxpathnamespace-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SimpleXMLElement::registerXPathNamespace</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$prefix</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$namespace</code></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   Creates a prefix/ns context for the next XPath query. In particular, this is
   helpful if the provider of the given XML document alters the namespace
   prefixes. <code class="literal">registerXPathNamespace</code> will create a prefix for
   the associated namespace, allowing one to access nodes in that namespace 
   without the need to change code to allow for the new prefixes dictated by the
   provider.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-simplexmlelement.registerxpathnamespace-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">prefix</code></dt>

     <dd>

      <p class="para">
       The namespace prefix to use in the XPath query for the namespace given in 
       <code class="parameter">namespace</code>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">namespace</code></dt>

     <dd>

      <p class="para">
       The namespace to use for the XPath query. This must match a namespace in
       use by the XML document or the XPath query using 
       <code class="parameter">prefix</code> will not return any results.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-simplexmlelement.registerxpathnamespace-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-simplexmlelement.registerxpathnamespace-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5463">
    <p><strong>示例 #1 Setting a namespace prefix to use in an XPath query</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$xml&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOD<br /></span><span style="color: #DD0000">&lt;book&nbsp;xmlns:chap="http://example.org/chapter-title"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;My&nbsp;Book&lt;/title&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;chapter&nbsp;id="1"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;chap:title&gt;Chapter&nbsp;1&lt;/chap:title&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;para&gt;Donec&nbsp;velit.&nbsp;Nullam&nbsp;eget&nbsp;tellus&nbsp;vitae&nbsp;tortor&nbsp;gravida&nbsp;scelerisque.&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In&nbsp;orci&nbsp;lorem,&nbsp;cursus&nbsp;imperdiet,&nbsp;ultricies&nbsp;non,&nbsp;hendrerit&nbsp;et,&nbsp;orci.&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nulla&nbsp;facilisi.&nbsp;Nullam&nbsp;velit&nbsp;nisl,&nbsp;laoreet&nbsp;id,&nbsp;condimentum&nbsp;ut,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ultricies&nbsp;id,&nbsp;mauris.&lt;/para&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/chapter&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;chapter&nbsp;id="2"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;chap:title&gt;Chapter&nbsp;2&lt;/chap:title&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;para&gt;Lorem&nbsp;ipsum&nbsp;dolor&nbsp;sit&nbsp;amet,&nbsp;consectetuer&nbsp;adipiscing&nbsp;elit.&nbsp;Proin&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gravida.&nbsp;Phasellus&nbsp;tincidunt&nbsp;massa&nbsp;vel&nbsp;urna.&nbsp;Proin&nbsp;adipiscing&nbsp;quam&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vitae&nbsp;odio.&nbsp;Sed&nbsp;dictum.&nbsp;Ut&nbsp;tincidunt&nbsp;lorem&nbsp;ac&nbsp;lorem.&nbsp;Duis&nbsp;eros&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tellus,&nbsp;pharetra&nbsp;id,&nbsp;faucibus&nbsp;eu,&nbsp;dapibus&nbsp;dictum,&nbsp;odio.&lt;/para&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/chapter&gt;<br />&lt;/book&gt;<br /></span><span style="color: #007700">EOD;<br /><br /></span><span style="color: #0000BB">$sxe&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xml</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$sxe</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerXPathNamespace</span><span style="color: #007700">(</span><span style="color: #DD0000">'c'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'http://example.org/chapter-title'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sxe</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'//c:title'</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$title</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$title&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Chapter 1
Chapter 2
</pre></div>
    </div>
    <div class="example-contents"><p>
     Notice how the XML document shown in the example sets a namespace with a 
     prefix of <code class="literal">chap</code>. Imagine that this document (or another
     one like it) may have used a prefix of <code class="literal">c</code> in the past for
     the same namespace. Since it has changed, the XPath query will no longer 
     return the proper results and the query will require modification. Using 
     <code class="literal">registerXPathNamespace</code> avoids future modification of the
     query even if the provider changes the namespace prefix.
    </p></div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-simplexmlelement.registerxpathnamespace-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="simplexmlelement.xpath.html" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::xpath()</a> - Runs XPath query on XML data</span></li>
    <li class="member"><span class="methodname"><a href="simplexmlelement.getdocnamespaces.html" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::getDocNamespaces()</a> - Returns namespaces declared in document</span></li>
    <li class="member"><span class="methodname"><a href="simplexmlelement.getnamespaces.html" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::getNamespaces()</a> - Returns namespaces used in document</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>